#include "phoneloginwidget.h"
#include "loginwidget.h"
#include <QTimer>

PhoneLoginWidget::PhoneLoginWidget(bool IsLoginMode,QWidget *parent)
    : QWidget{parent}
{
    //1.设置属性和布局管理器
    this->setWindowIcon(QIcon(":/myResource/Image/WeChat.svg"));
    this->setFixedSize(300,300);
    this->setAttribute(Qt::WA_DeleteOnClose);

    QGridLayout* layout = new QGridLayout(this);
    layout->setSpacing(5);

    //2.添加控件
    QFont font("微软雅黑",30,400);

    titleTag = new QLabel;
    titleTag->setStyleSheet("QLabel{color:rgb(100,100,100);}");
    titleTag->setFixedHeight(50);
    titleTag->setAlignment(Qt::AlignHCenter|Qt::AlignTop);
    titleTag->setFont(font);

    verifyCodeEidt = new QLineEdit;
    PhoneEidt = new QLineEdit;
    auto handleEdit = [](QLineEdit* edit,const QString& text)
    {
        edit->setPlaceholderText(text);
        edit->setFixedHeight(30);
        edit->setStyleSheet("QLineEdit{border:none;border-radius:5px;padding-left:5px;}");
    };
    handleEdit(PhoneEidt,"输入手机号");
    handleEdit(verifyCodeEidt,"输入验证码");

    sendVerifyBtn = new QPushButton;
    sendVerifyBtn->setFixedHeight(30);
    sendVerifyBtn->setText("发送验证码");
    submitBtn = new QPushButton;
    submitBtn->setFixedHeight(40);
    QString style = "QPushButton{border:1px solid grey;background-color:rgb(44, 182, 61);border-radius:5px;}";
    style+="QPushButton:hover{background-color:rgb(34, 172, 51);}";
    style+="QPushButton:pressed{background-color:rgb(24, 162, 41);}";
    submitBtn->setStyleSheet(style);

    switchModBtn = new QPushButton;
    switchModBtn->setStyleSheet("QPushButton{border:none;}");
    switchUserBtn = new QPushButton;
    switchUserBtn->setStyleSheet("QPushButton{border:none;}");

    init(IsLoginMode);

    layout->addWidget(titleTag,0,0,1,3);
    layout->addWidget(PhoneEidt,1,0,1,3);
    layout->addWidget(verifyCodeEidt,2,0,1,2);
    layout->addWidget(sendVerifyBtn,2,2,1,1);
    layout->addWidget(submitBtn,3,0,1,3);
    layout->addWidget(switchModBtn,4,0,1,1);
    layout->addWidget(switchUserBtn,4,2,1,1);

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(sendVerifyBtn,&QPushButton::clicked,this,[=]()
    {
        dataCenter->setPhoneToChange(PhoneEidt->text());
        const QString& phone = dataCenter->getPhoneToChange();
        if(phone.isEmpty())
        {
            Toast::showMessage("手机号为空");
            return;
        }
        dataCenter->getVertifyCodeAsync(phone);
        QTimer* timer =  new QTimer;
        waitTime = 60;
        sendVerifyBtn->setEnabled(false);
        sendVerifyBtn->setText(QString::number(waitTime)+"s");
        connect(timer,&QTimer::timeout,this,[=]()
        {
            if(waitTime<=1)
            {
                sendVerifyBtn->setEnabled(true);
                sendVerifyBtn->setText("发送验证码");
                timer->stop();
                timer->deleteLater();
                return;
            }
            sendVerifyBtn->setText(QString::number(--waitTime)+"s");
        });
        timer->start(1000);

    });
    connect(submitBtn,&QPushButton::clicked,this,[=]()
    {
        const QString& phone = dataCenter->getPhoneToChange();
        const QString& verifyCodeId = dataCenter->getCurrentVerifyCodeId();
        const QString& verifyCode = verifyCodeEidt->text();
        if(phone.isEmpty()||verifyCode.isEmpty())
        {
            Toast::showMessage("未获取验证码或验证码为空");
            return;
        }
        if(this->isLoginMode)
        {
            connect(dataCenter,&model::DataCenter::phoneLoginDone,this,[=]()
            {
                MainWidget::getInstance()->show();
                this->close();
            });
            dataCenter->phoneLoginAsync(phone,verifyCodeId,verifyCode);
        }
        else
        {
            connect(dataCenter,&model::DataCenter::phoneRegisterDone,this,[=]()
            {
                init(true);
                Toast::showMessage("注册成功");
                recoverWaitTime();
            });
            dataCenter->phoneRegisterAsync(phone,verifyCodeId,verifyCode);
        }
    });
    //这里必须显示使用this调用isLoginMode，不然会使用isLoginMode的局部变量
    connect(switchModBtn,&QPushButton::clicked,this,[this]()
    {
        init(!this->isLoginMode);
    });
    connect(switchUserBtn,&QPushButton::clicked,this,[this]()
    {
        LoginWidget* loginWidget = new LoginWidget;
        if(!this->isLoginMode)
            loginWidget->switchMode();
        loginWidget->show();
        this->close();
    });

}

void PhoneLoginWidget::init(bool isLoginMode)
{
    this->isLoginMode = isLoginMode;
    verifyCodeEidt->clear();
    if(isLoginMode)
    {
        titleTag->setText("登录");
        this->setWindowTitle("登录");
        submitBtn->setText("登录");
        switchModBtn->setText("注册");
        switchUserBtn->setText("用户登录");
    }
    else
    {
        titleTag->setText("注册");
        this->setWindowTitle("注册");
        submitBtn->setText("注册");
        switchModBtn->setText("登录");
        switchUserBtn->setText("用户注册");
    }
}

void PhoneLoginWidget::recoverWaitTime()
{
    waitTime = 1;
}
